#!/bin/sh /etc/rc.common

START=65

. /etc/functions.sh

ENABLE_WIFIDOG=0

isEnableServer()
{
	local enable	
	config_get enable $1 wifidog_enable	
	if [ 1 -eq $enable ]; then
		ENABLE_WIFIDOG=1
	fi	
}

do_config(){	
	local gateway_interface
	local gateway_host
	local allowed_devices
	local allowed_ip_list
	
	config_get gateway_interface $1 gateway_interface
	config_get gateway_host $1 gateway_host
	config_get allowed_devices $1 allowed_devices 
	config_get allowed_ip_list $1 allowed_ip_list 
	
	sed -e "s#|GATEWAYID|#$gateway_interface#g" \
		-e "s#|REDIRECTURL|#$gateway_host#g" \
		-e "s#|ALLOWEDDEVICES|#$allowed_devices#g" \
		-e "s#|ALLOWEDIPLIST|#$allowed_ip_list#g" \
		/etc/wifidog/wifidog.conf.template > /etc/wifidog.conf
}

start(){
	
	config_load wifidog
	echo "start get the parameter" > /dev/console
	config_foreach isEnableServer settings	
	echo "start jugement" > /dev/console
	if [ $ENABLE_WIFIDOG -eq 1 ];then
		config_foreach do_config settings
		echo "Starting the wifidog"
		/usr/bin/wifidog-init start
	fi
	
}

stop(){

	ps |grep wifidog |grep -v grep
	if [ $? -eq 0 ];then
		/usr/bin/wifidog-init stop
		if [ $? -eq 0 ];then
			echo "The wifidog has been stoped"
		else
			killall -9 wifidog
		fi
	else
		echo "There is no wifidog deamon running!" 
	fi
	
}
restart(){
	
	stop
	sleep 1
	start
	
}






